package com.ruijie.autotest.log.common.crypto;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

public interface CipherInitializer {
	Cipher createCipher(int cryptoMode);

	default ICryptoFactory cryptoFactory() {
		return new ICryptoFactory() {
			@Override
			public Crypto encryption() {
				return rawBytes -> doCipher(Cipher.ENCRYPT_MODE, rawBytes);
			}

			@Override
			public Crypto decryption() {
				return rawBytes -> doCipher(Cipher.DECRYPT_MODE, rawBytes);
			}

			public byte[] doCipher(int cryptoMode, byte[] rawBytes) {
				try {
					return createCipher(cryptoMode).doFinal(rawBytes);
				} catch (IllegalBlockSizeException | BadPaddingException e) {
					throw new RuntimeException("doCipher failed.", e);
				}
			}
		};
	}
}
